/**
 * Copyright (c) 2023 myons Inc. All rights reserved.
 */
package cn.zmwh.portal.listener;

import cn.hutool.core.util.StrUtil;
import cn.zmwh.common.core.config.RedisConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;

import java.util.HashSet;

/**
 * @describe: redis过期回调 、
 * @author: dmzmwh 、
 * @time: 2023-06-25 9:35 、
 */
@Slf4j
@Configuration
public class RedisListenerConfig {

    @Autowired
    private RedisKeyOutListener listener;
    @Autowired
    private RedisConfig redisConfig;

    /**
     * 回调监听
     *
     * @return
     */
    @Bean
    public RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory connectionFactory) {
        RedisMessageListenerContainer redisMessageListenerContainer = new RedisMessageListenerContainer();
        redisMessageListenerContainer.setConnectionFactory(connectionFactory);
        HashSet<PatternTopic> set = new HashSet<>();
        String expired = StrUtil.format("__keyevent@{}__:expired", redisConfig.getEx());
        log.info("回调" + expired);
        set.add(new PatternTopic(expired));
        redisMessageListenerContainer.addMessageListener(listener, set);
        return redisMessageListenerContainer;
    }

}
